package com.neuedu.autosale.purchase.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;
import com.neuedu.autosale.purchase.entity.Restock;
import com.neuedu.autosale.purchase.service.RestockService;
import com.neuedu.autosale.sys.entity.Provider;
import com.neuedu.autosale.sys.entity.Storage;

@Controller
public class RestockController {
	@Autowired
	private RestockService restockService;

	@RequestMapping("restock/index")
	public String stockIndex() {
		return "restock/restocklist";
	}

	@RequestMapping("restock/restocklist")
	@ResponseBody
	public Pager<Restock> restockList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows, HttpServletRequest request) {

		String carName = request.getParameter("carName");
		String providerId = request.getParameter("providerId");
		String storageId = request.getParameter("storageId");

		System.out.println(carName + providerId + storageId);

		Pager<Restock> pager = new Pager<>(page, rows);

		Restock restock = new Restock();

		if (carName != null && carName != "") {
			restock.setCarName(carName);
			pager.setCondition(restock);
		}

		if (providerId != null && providerId != "") {
			Provider provider = new Provider(providerId);
			restock.setProvider(provider);
			pager.setCondition(restock);
		}

		if (storageId != null && storageId != "") {
			Storage storage = new Storage(storageId);
			restock.setStorage(storage);
			pager.setCondition(restock);
		}

		List<Restock> restocklist = restockService.findListByPager(pager);
		int total = restockService.findTotalByPager(pager);
		pager.setRows(restocklist);
		pager.setTotal(total);
		return pager;
	}

	@RequestMapping("restock/add")
	@ResponseBody
	public Result doAdd(MultipartFile imageFile, Restock restock) {

		Result result = restockService.add(restock);
		return result;
	}

	@RequestMapping("restock/edit")
	@ResponseBody
	public Result doEdit(MultipartFile imageFile, Restock restock) {

		Result result = restockService.edit(restock);
		return result;
	}

	@RequestMapping("restock/delete")
	@ResponseBody
	public Result doDelete(Integer restockId) {
		List<Restock> list = new ArrayList<Restock>();
		list.add(new Restock(restockId));
		Result result = restockService.deleteByIds(list);
		return result;
	}

}
